/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.suypower.carManage.json;

import com.jarp.db.DAO;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.suypower.carManage.VO.TollsVO;
import com.suypower.carManage.comm.ConstUtil;
import com.suypower.carManage.comm.SQLHelp;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import net.sf.json.JSONArray;
import org.apache.commons.lang3.StringUtils;

/**
 *
 * @author hythzx
 */
public class TollsJson extends ActionSupport {

    private String car_code;
    private String stime;
    private String etime;
    private String start = "1";
    private String limit = "20";
    DAO dao = new DAO();

    /**
     *
     * @return
     */
    @Override
    public String execute() {
        String sql = this.getSQL();
        int iStart = Integer.parseInt(start);
        int iPageSize = Integer.parseInt(limit);
        int iPage = iStart / iPageSize + 1;
        try {
            Vector v = dao.getDataSet(ConstUtil.DBNAME, sql + " order by t.id desc ", iPage, iPageSize);
            List<TollsVO> list = new ArrayList<TollsVO>();
            if (v != null) {
                Iterator<Hashtable> iterator = v.iterator();
                while (iterator.hasNext()) {
                    Hashtable hashtable = iterator.next();
                    TollsVO tollsVO = new TollsVO();
                    tollsVO.setCar_code((String) hashtable.get("CAR_CODE"));
                    tollsVO.setCost((String) hashtable.get("COST"));
                    tollsVO.setFee_date((String) hashtable.get("FEE_DATE"));
                    tollsVO.setId((String) hashtable.get("ID"));
                    tollsVO.setRemark((String) hashtable.get("REMARK"));
                    tollsVO.setTolls_type((String) hashtable.get("TOLLS_TYPE"));
                    tollsVO.setTolls_type_name((String) hashtable.get("TOLLS_TYPE_NAME"));
                    list.add(tollsVO);
                }
                String totalCountSQL = "select count(1) CNT from (" + sql + " ) zz ";
                String totalCount = dao.getDataValue(ConstUtil.DBNAMEODS, totalCountSQL);
                JSONArray json = JSONArray.fromObject(list);
                ActionContext.getContext().put("data", "{totalCount:" + totalCount + ",data:" + json.toString() + "}");
            } else {
                ActionContext.getContext().put("data", "{totalCount:" + 0 + ",data:''}");
            }
        } catch (ServletException ex) {
            Logger.getLogger(TollsJson.class.getName()).log(Level.SEVERE, null, ex);
        }
        return SUCCESS;
    }

    private String getSQL() {
        StringBuilder sql = new StringBuilder("SELECT t.*,a.name AS tolls_type_name FROM tb_tolls t LEFT JOIN tb_metadata a ON t.tolls_type = a.value WHERE a.code ='tolls' ");
        if (!StringUtils.isEmpty(car_code)) {
            sql.append(" and t.car_code in  (").append(SQLHelp.splitStrFromWeb(car_code)).append(")");
        }
        if (!StringUtils.isEmpty(stime)) {
            sql.append(" AND t.fee_date >= '").append(stime).append("'");
        }
        if (!StringUtils.isEmpty(etime)) {
            sql.append(" AND t.fee_date <= '").append(etime).append("'");
        }
        return sql.toString();
    }

    public String getCar_code() {
        return car_code;
    }

    public void setCar_code(String car_code) {
        this.car_code = car_code;
    }

    public String getStime() {
        return stime;
    }

    public void setStime(String stime) {
        this.stime = stime;
    }

    public String getEtime() {
        return etime;
    }

    public void setEtime(String etime) {
        this.etime = etime;
    }

    public String getStart() {
        return start;
    }

    public void setStart(String start) {
        this.start = start;
    }

    public String getLimit() {
        return limit;
    }

    public void setLimit(String limit) {
        this.limit = limit;
    }

}
